package com.cn.namesrv.processor;

import com.cn.common.*;
import com.cn.common.processor.AbstractProcessor;
import com.cn.common.processor.INettyRequestProcessor;
import com.cn.common.topic.TopicManager;
import com.cn.msg.Message;
import com.cn.namesrv.NameSrvController;
import com.cn.util.GsonUtil;
import com.cn.util.RemotingSerializable;
import io.netty.channel.ChannelHandlerContext;

import java.nio.charset.Charset;
import java.util.*;

/**
 * nameSrv节点
 */
public class NameSrvDefaultProcessor extends AbstractProcessor implements INettyRequestProcessor {

    private NameSrvController nameSrvController;

    public NameSrvDefaultProcessor(NameSrvController nameSrvController) {
        this.nameSrvController = nameSrvController;
    }

    @Override
    public Message process(Message message, ChannelHandlerContext ctx) {
        if (message.getType() == 2) {//broker节点注册
            decodeBody(message);
            BrokerEntity brokerEntity = new BrokerEntity();
            brokerEntity = (BrokerEntity) GsonUtil.convertJson2Obj(message.getBody(), brokerEntity);
            System.out.println("broker节点地址：" + brokerEntity.getAddress());
            //将注册信息放到缓存中
            BrokersUrlStore.put(brokerEntity.getAddress());
            BrokerAppStore.put(ctx, brokerEntity);
            BrokerCluster.put(brokerEntity.getCluster_name(), brokerEntity);

            //新代码
            {
                nameSrvController.getTopicRouteManager().registerBroker(brokerEntity, ctx);
            }

            autoOk(message);

        } else if (message.getType() == 4) {
            //broker拉取主题topic
            String body = "{\"topic\":\"demo\"}";
            message.setType((byte) 4);
            message.setByteBodys(body.getBytes(Charset.forName("UTF-8")));

        } else if (message.getType() == 6) {
            //broker节点 拉取集群信息
            decodeBody(message);
            if (BrokerCluster.getCluster(message.getBody()) != null) {
                Set<BrokerEntity> set = BrokerCluster.get(message.getBody());
                RemotingSerializable.encode(null);
                message.setByteBodys(RemotingSerializable.encode(set));
            }

        } else if (message.getType() == 5) {
            //消费端拉取topic信息
//            System.out.println("消费端拉取topic:");
            return null;

        } else if (message.getType() == 14) {
            //注册topic
            decodeBody(message);
            nameSrvController.getTopicRouteManager().registerTopic(nameSrvController.getTopicManager().decode(message.getBody()), ctx);
            return null;
        } else if (message.getType() == 15) {
            //拉取topic
            decodeBody(message);
            message.setByteBodys(nameSrvController.getTopicRouteManager().filterTopicRouteData(message.getBody()));
            message.setLength(message.getByteBodys().length);//长度
            message.setBody(null);
        } else if (message.getType() == 19) {
            message.setByteBodys(nameSrvController.getTopicRouteManager().pullAllTopic());//拉取所有的topic
            message.setLength(message.getByteBodys().length);//长度
            message.setBody(null);
        }
        return message;
    }

}
